package edu.cornell.cs4740.wsd.tools;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

import edu.cornell.cs4740.wsd.parsing.TrainingData;

public class EvaluationSetCreator {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String trainingPath = args[0];
		int sizeOfEvaluationSet = Integer.parseInt(args[1]);
		
		// ------- READ IN TRAINING DATA 
		List<TrainingData> trainingDataFull = new ArrayList<TrainingData>();
		
		try {
			Scanner trainingScanner = new Scanner(new File(trainingPath));
			while(trainingScanner.hasNext()) {
				trainingDataFull.add(new TrainingData(trainingScanner.nextLine(), false));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("training data read");
		
		Collections.shuffle(trainingDataFull);
		List<TrainingData> evaluationData = trainingDataFull.subList(0, sizeOfEvaluationSet);
		
		// everything else becomes the real training data
		List<TrainingData> trainingData = trainingDataFull.subList(sizeOfEvaluationSet, trainingDataFull.size());
		
		// sanity check to make sure we aren't missing stuff
		System.out.println(trainingData.size() + " training data samples");
		System.out.println(evaluationData.size() + " evaluation data samples");
		
		stripEvaluationDataOfSenseInfo(evaluationData);

		
		// output new training data
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(new File(trainingPath + ".new")));
			for(TrainingData td : trainingData) {
				bw.write(td.toTrainingDataString());
				bw.newLine();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	private static void stripEvaluationDataOfSenseInfo(List<TrainingData> evaluationData) {
		List<Boolean> nullSenses = new ArrayList<Boolean>();
		for(int x = 0; x < 5; x++) {
			nullSenses.add(Boolean.FALSE);
		}

		for(TrainingData td : evaluationData) {
			td.setSenseUsage(nullSenses);
		}
	}
}
